package cn.mabach.cms.service.impl;



import cn.mabach.cms.dao.TopicCategoryDao;
import cn.mabach.cms.entity.TopicCategoryEntity;
import cn.mabach.cms.service.TopicCategoryService;
import cn.mabach.result.PageResult;
import cn.mabach.result.RS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
public class TopicCategoryServiceImpl extends ServiceImpl<TopicCategoryDao, TopicCategoryEntity> implements TopicCategoryService {

    @Override
    public RS<PageResult<TopicCategoryEntity>> queryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        if (pageSize>20){
            return RS.error();
        }

        IPage<TopicCategoryEntity> page = this.page(
                new Page<TopicCategoryEntity>(pageNum,pageSize),
                new QueryWrapper<TopicCategoryEntity>().like("name",keyword)
        );

        return RS.ok(new PageResult(page));
    }




    @Override
    public RS<TopicCategoryEntity> getByIdE(@RequestParam("id") Long id) {
            TopicCategoryEntity res = this.getById(id);

        return RS.ok(res);
    }

    @Override
    public RS saveE(@RequestBody  TopicCategoryEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return RS.error();
        }
        return RS.ok();
    }

    @Override
    public RS updateByIdE(@RequestBody TopicCategoryEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return RS.error();
        }
        return RS.ok();
    }


    @Override
    public RS removeOneById(@RequestParam("id") Long id) {
        boolean b = this.removeById(id);
        if (!b){
            return RS.error();
        }
        return RS.ok();
    }

    @Override
    @Transactional
    public RS removeByIdsE(@RequestBody List<Long> ids) {
        boolean b = this.removeByIds(ids);
        if (!b){
            return RS.error();
        }
        return RS.ok();
    }

}